﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BussinessEntity;
using BussinessLayer;
using Configurations;

namespace PresentationLayer
{
    public partial class frmSearchBill : DevComponents.DotNetBar.OfficeForm
    {
        public List<Receipt> Result { get; set; }
        public List<Receipt> list = ReceiptManager.GetAll().Array;

        public frmSearchBill()
        {
            InitializeComponent();
            cbSearchBy.Items.Add("Tên khách hàng");
            cbSearchBy.Items.Add("Số điện thoại");
            cbSearchBy.Items.Add("Trạng thái");
            cbSearchBy.Items.Add("Ngày nhận");
            cbSearchBy.SelectedIndex = 0;
        }

        private void cbSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSearchBy.SelectedIndex != 3)
            {
                cbKeywork.Visible = true;
                dtiDate.Visible = false;
                if (cbSearchBy.SelectedIndex != 2)
                {
                    cbKeywork.DataSource = null;
                    cbKeywork.DropDownStyle = ComboBoxStyle.DropDown;
                    cbKeywork.DataSource = list;
                    if (cbSearchBy.SelectedIndex == 0)
                    {
                        cbKeywork.DisplayMember = cbKeywork.ValueMember = "CustomerName";
                        foreach (var item in list)
                        {

                            cbKeywork.AutoCompleteCustomSource.Add(item.CustomerName);
                        }
                    }
                    else
                        if (cbSearchBy.SelectedIndex == 1)
                        {
                            foreach (var item in list)
                            {
                                cbKeywork.AutoCompleteCustomSource.Add(item.PhoneNumber);
                            }
                            cbKeywork.DisplayMember = cbKeywork.ValueMember = "PhoneNumber";
                        }
                    cbKeywork.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    cbKeywork.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
                else
                {
                    cbKeywork.DropDownStyle = ComboBoxStyle.DropDownList;
                    cbKeywork.DataSource = null;
                    cbKeywork.Items.Add(BillState.DaNhanTB);
                    cbKeywork.Items.Add(BillState.DaSua);
                    cbKeywork.Items.Add(BillState.DaThanhToan);
                    cbKeywork.SelectedIndex = 0;
                }
            }
            else
            {
                cbKeywork.Visible = false;
                dtiDate.Visible = true;
                dtiDate.Value = DateTime.Today;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Result = null;
            Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cbSearchBy.SelectedIndex != 3)
            {
                if (cbSearchBy.SelectedIndex == 0)
                {
                    Result = list.FindAll(s => s.CustomerName.Contains(cbKeywork.Text));
                }
                else
                    if (cbSearchBy.SelectedIndex == 1)
                    {
                        Result = list.FindAll(s => s.PhoneNumber.Contains(cbKeywork.Text));
                    }
                    else
                    {
                        Result = list.FindAll(s => s.Status == cbKeywork.Text);
                    }
            }
            else
                Result = list.FindAll(s => s.DateReceiced == dtiDate.Value);
            if (Result.Count > 0)
                Close();
            else
            {
                MessageBox.Show("Không tìm thấy thông tin phù hợp", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}